import util from '@/libs/util';
import Cookies from 'js-cookie';
import stundentDetail from './stundentDetail.vue';
import classesSchedule from '../classes-release/classes-schedule.vue';
import dateUtil from '../../util/getDate';

export default {
    components: {
        stundentDetail, classesSchedule
    },
    data () {
        return {
            columns: [{
                width: 55,
                type: 'selection',
                align: 'center'
            }, {
                title: "余额(元)",
                key: 'balance'
            }, {
                title: '姓名',
                key: 'name'
            }, {
                title: '性别',
                key: 'sex',
                render: (h, params) => {
                    return h('div', {}, params.row.sex === 'MAN' ? '男' : (params.row.sex === 'WOMAN' ? '女' : '未知'));
                }
            }, {
                title: '出生日期',
                key: 'birthdate',
                render: function (h, params) {
                    return h('span', {}, dateUtil.getDate(params.row.birthdate, 'date'));
                }
            }, {
                title: '证件号',
                key: 'cardcode'
            }, /* {
                title: '点到卡号',
                key: 'namingcode'
            }, */ {
                title: '家长姓名',
                key: 'parentname'
            }, {
                title: '家长手机号',
                key: 'parentphone'
            }, {
                title: '就读年级',
                key: 'gradeid',
                render: (h, params) => {
                    return h('div', {}, params.row.grade ? params.row.grade.name : '');
                }
            }, {
                title: '是否报过名',
                key: 'issignup',
                render: (h, params) => {
                    return h('div', {}, params.row.issignup === 'YES' ? '是' : '否');
                }
            }, {
                title: '操作',
                key: 'action',
                width: 200,
                align: 'center',
                render: (h, params) => {
                    return h('div', [
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.detail(params.row.id);
                                }
                            }
                        }, '查看'),
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.showEdit(params.row.id);
                                }
                            }
                        }, '修改'),
                        h('Button', {
                            props: {
                                type: 'error',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.delete(params.row.id);
                                }
                            }
                        }, '删除'),
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.classScheduleVisible = true;
                                    // let teacherId = params.row.mainTeacherId;
                                    // let classSchedule = params.row.classSchedules;
                                    // let classRoomId;
                                    // if (classSchedule != null && classSchedule != '' && classSchedule.length > 0) {
                                    //     classRoomId = classSchedule[0].classRoomId;
                                    // }
                                    // this.$refs.classesSchedule.teacherId = teacherId;
                                    // this.$refs.classesSchedule.classRoomId = classRoomId;
                                    this.entity.id = params.row.id;
                                }
                            }
                        }, '管理课表')
                    ]);
                }
            }],
            baseuser: JSON.parse(Cookies.get('baseUser')),
            data: [],
            formData: {},
            query: {},
            selectYesOrNo: [{id: 'YES', name: '是'}, {id: 'NO', name: '否'}],
            sexList: [{id: 'MAN', name: '男'}, {id: 'WOMAN', name: '女'}],
            gradeList: [],
            ids: [],
            formDataRules: {
                name: [
                    { required: true, message: '请输入姓名', trigger: 'blur' }
                ],
                birthdate: [
                    { required: true, message: '请选择出生日期', trigger: 'change', type: 'date'}
                ],
                parentphone: [
                    { required: true, message: '请输入家长手机号', trigger: 'blur', type: 'string'}
                ],
                areaId: [
                    { required: true, message: '请选择校区', trigger: 'change', type: 'number'}
                ]
            },
            total: 0,
            pageIndex: 1,
            pageSize: 10,
            editModal: false,
            editTitle: '新增学员',
            isRead: false,
            viewModal: false,
            classScheduleVisible: false,
            entity: {}
        };
    },
    mounted () {
        this.loadDictionaries();
    },
    methods: {
        loadDictionaries () {
            util.ajax.post('/base/datadictionary/findByCode', util.toFormData({
                CODE: 'GRADE'
            })).then(res => {
                this.gradeList = res.data.data;
            });
            this.loadList();
        },
        loadList () {
            util.ajax.post('/busine/student/list', util.toFormData({
                page: this.pageIndex,
                size: this.pageSize,
                organizeid: this.baseuser.orgId,
                name: this.query.name ? this.query.name : '',
                cardcode: this.query.cardcode ? this.query.cardcode : '',
                parentphone: this.query.parentphone ? this.query.parentphone : '',
                familyphone: this.query.familyphone ? this.query.familyphone : '',
                namingcode: this.query.namingcode ? this.query.namingcode : '',
                gradeStart: this.query.gradeStart ? this.query.gradeStart : '',
                gradeEnd: this.query.gradeEnd ? this.query.gradeEnd : '',
                sex: this.query.sex ? this.query.sex : ''
            })).then(res => {
                this.data = res.data.data.list;
                this.total = res.data.data.total;
            });
        },
        refresh () {
            this.query = {};
            this.loadDictionaries();
        },
        showAdd () {
            this.formData.birthdate = '';
            this.editTitle = '新增学员';
            this.formData = {
                organizeid: this.baseuser.orgId
            };
            this.editModal = true;
            this.isRead = false;
            util.ajax.post('/base/datadictionary/findByCode', util.toFormData(// 获得区域
                {CODE: 'AREA_TYPE'}
            )).then(res => {
                if (res.data.code == 200) {
                    var data = res.data.data;
                    this.areaList = data;
                } else {
                    this.$Message.error(res.data.message);
                }
            });
        },
        detail (id) {
            this.editTitle = '学员信息';
            this.formData.id = id;
            this.viewModal = true;
            this.isRead = true;
        },
        showEdit (id) {
            this.editTitle = '修改学员';
            util.ajax.post('/busine/student/detail', util.toFormData({
                id: id
            })).then(res => {
                this.formData = res.data.data;
                this.editModal = true;
                this.isRead = false;
            });
        },
        delete (id) {
            this.$Modal.confirm({
                content: '是否确认删除？',
                onOk: () => {
                    util.ajax.post('/busine/student/delete', util.toFormData({
                        id: id
                    })).then(res => {
                        if (res.data.message === 'SUCCESS') {
                            this.$Message.success('删除成功！');
                            this.loadList();
                            this.editModal = false;
                        }
                    });
                }
            });
        },
        save () {
            let formData = this.formData;
            formData.birthdate = (!formData.birthdate ? '' : new Date(formData.birthdate));
            formData.gradeid = formData.gradeid ? formData.gradeid : '';
            this.$refs['formData'].validate((valid) => { // 表单验证
                if (valid) {
                    util.ajax.post('/busine/student/save', util.toFormData(formData)).then(res => {
                        if (res.data.message === 'SUCCESS') {
                            this.$Message.success('保存成功！');
                            this.loadList();
                            this.editModal = false;
                        }
                    });
                } else {
                    this.$Message.warning('请检查输入项！');
                }
            });
        },
        selectChange (selection) {
            this.ids = [];
            selection.forEach(item => {
                this.ids.push(item.id);
            });
        },
        deleteByIds () {
            if (this.ids.length === 0) {
                this.$Modal.error({
                    content: '至少选择一条数据'
                });
                return;
            }
            this.$Modal.confirm({
                content: '是否确认批量删除？',
                onOk: () => {
                    util.ajax.post('/busine/student/deleteByIds', util.toFormData({
                        ids: this.ids.toString()
                    })).then(res => {
                        if (res.data.message === 'SUCCESS') {
                            this.$Message.success('批量删除成功！');
                            this.loadList();
                            this.editModal = false;
                        }
                    });
                }
            });
        }
    }
};
